package com.example.com.hupu.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.com.hupu.R;
import com.example.com.hupu.activity.NewsDetailActivity;
import com.example.com.hupu.bean.NewsItem;

import java.util.List;

/**
 * 新闻Adapter
 */
public class NBANewsAdapter extends RecyclerView.Adapter<NBANewsAdapter.ViewHolder> {

    public List<NewsItem> mResult;

    public NBANewsAdapter(List<NewsItem> result) {
        this.mResult = result;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.nbanews_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        final NewsItem newsItem = mResult.get(position);
        holder.tv_title.setText(newsItem.title);
        Glide.with(holder.iv_nbanews).load(newsItem.imgurl).into(holder.iv_nbanews);
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(holder.iv_nbanews.getContext(), NewsDetailActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(NewsDetailActivity.URL, newsItem.url);
                holder.mView.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mResult == null ? 0 : mResult.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        ImageView iv_nbanews;
        View mView;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            tv_title = view.findViewById(R.id.tv_title);
            iv_nbanews = view.findViewById(R.id.iv_nbanews);
        }
    }

}